from datetime import datetime, date

# 不同天的时间差

time_1 = '2022-06-22 16:06:40'

time_2 = '2022-06-23 03:37:47'

def calculate_timegap(time_1=None, time_2=None):
    if time_1 is None:
        time_1 = input('time_1:')
    if time_2 is None:
        time_2 = input('time_2:')

    time_1_struct = datetime.strptime(time_1, "%Y-%m-%d %H:%M:%S")
    time_2_struct = datetime.strptime(time_2, "%Y-%m-%d %H:%M:%S")

    # 来获取时间差中的秒数。注意，seconds获得的秒只是时间差中的小时、分钟和秒部分，没有包含天数差，total_seconds包含天数差
    # 所以total_seconds两种情况都是可以用的
    total_seconds = (time_2_struct - time_1_struct).total_seconds()

    d = total_seconds / 60 / 60 / 24
    h = (d - int(d)) * 24
    m = (h - int(h)) * 60
    s = (m - int(m)) * 60
    out = '%dd%dh%dm%ds'%(int(d), int(h), int(m), int(s))
    while True:
        if out.startswith('0'):
            out = out[2:]
        else:
            break
    print(out)

if __name__ == '__main__':
    calculate_timegap()
